package cn.otrue.patienthealthmanager.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;
import cn.otrue.patienthealthmanager.R;
import cn.otrue.patienthealthmanager.adapter.Doctorpinglundpater;
import cn.otrue.patienthealthmanager.bean.TransferParameter;
import cn.otrue.patienthealthmanager.http.HttpCons;
import cn.otrue.patienthealthmanager.http.HttpServer;
import cn.otrue.patienthealthmanager.http.ServerHandler;
import cn.otrue.patienthealthmanager.view.XListView;
import cn.otrue.patienthealthmanager.view.XListView.IXListViewListener;

import com.google.gson.reflect.TypeToken;

/**
 * 健康社区
 * 
 * @author G
 * 
 */
public class YishengpinglunActivity extends BaseActivity implements
		IXListViewListener {

	private List<Map<String, String>> goods;
	private Doctorpinglundpater postAdapter;
	private int currentPage = 1;

	private String sectionName;
	private XListView lv_community_partition;



	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_yisheng_pinglun);

		initView();
	}

	private void initView() {
		goods = new ArrayList<Map<String, String>>();
		lv_community_partition = (XListView) findViewById(R.id.lv_community_partition);
		lv_community_partition.setPullLoadEnable(false); // 是否开启上拉加载
		lv_community_partition.setXListViewListener(this);// 上啦加载和下拉刷新的监听
	
		getDate();
		getpinglun(currentPage);
		
		initTitleBar();
	

	}



	public void initTitleBar() {
		TextView tv_title_title = (TextView) findViewById(R.id.tv_title_title);
		tv_title_title.setText(R.string.compppp);
	
		

		TextView tv_title_back = (TextView) findViewById(R.id.tv_title_back);
		tv_title_back.setVisibility(View.VISIBLE);
		tv_title_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});

	}

	private void getDate() {
	aliasAccount = getIntent().getStringExtra("aliasAccount");
	
	}


	private Handler faHandler = new ServerHandler() {
		@Override
		public void onSuccess(Bundle data, String state, String msg) {
			onLoad();
			if (state.equals("001")) {
				String tuu = data.getString(HttpCons.DATA);
				
				List<Map<String, String>> gooo = HttpServer.gson.fromJson(tuu,
						new TypeToken<List<Map<String, String>>>() {
						}.getType());
				if (gooo.size()>5) {
					lv_community_partition.setPullLoadEnable(true);
				}
				if (gooo!=null&&gooo.size()>0) {
					goods.addAll(gooo);
					postAdapter = new Doctorpinglundpater(goods,
							YishengpinglunActivity.this);
					lv_community_partition.setAdapter(postAdapter);
					Toast.makeText(YishengpinglunActivity.this, "操作成功", 0).show();
				}else{
					
					Toast.makeText(YishengpinglunActivity.this, "暂无数据", 0).show();
				}
			
				
				
			} else {
				onLoad();
				Toast.makeText(YishengpinglunActivity.this, msg, 0).show();
			}
		}

		@Override
		public void onFail(Bundle data, String state) {
			onLoad();
			if (state.equals(HttpCons.STATE_EXCEPTION)) {
				// 代表进入状态STATE_EXCEPTION 网络异常
				Toast.makeText(YishengpinglunActivity.this, "网络异常,请稍后重试",
						Toast.LENGTH_SHORT).show();

			} else if (state.equals(HttpCons.STATE_FORMAT_ERROR)) {
				// 代表进入STATE_FORMAT_ERROR(不是Json)
			} else {
				// 代表进入STATE_ANALYTICAL_ERROR(Json解析错误)

			}
		}
	};
	private String aliasAccount;



	private void onLoad() {
		lv_community_partition.stopRefresh();
		lv_community_partition.stopLoadMore();
		lv_community_partition.setRefreshTime("刚刚");
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
	

		default:
			break;
		}

	}

	private void getpinglun(int i) {
		
		HashMap<String, Object> map = new HashMap<String, Object>();
		
		TransferParameter parameter = new TransferParameter();
		parameter.setUsername(aliasAccount);
		parameter.setCurrentPage(i + "");
		parameter.setPagesize("20");
		parameter.setPassword(" ");
		parameter.setData(map);
		new HttpServer().post(HttpCons.queryTheDoctorEvaluationInformation, parameter, faHandler);
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		currentPage = 1;
		goods.clear();
		getpinglun(currentPage);
		}



	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		currentPage++;
		
		
		
		getpinglun(currentPage);
	
	}

}
